﻿using System;
using CosyTrade.Data;
using CosyTrade.Organization;
using CosyTrade.UI;
using CosyTrade.Distribution;
using DevExpress.XtraEditors.Controls;
using CosyTrade.Store;
using System.Collections.Generic;

namespace CosyTrade.ClientForm.Distribution
{
    public partial class OrderReclaimDetail : DetailFormBase
    {
        public OrderReclaimDetail(): this(Guid.Empty)
        {}
        public OrderReclaimDetail( Guid extKey) : base(extKey)
        {
            InitializeComponent();
            selectEmployee1.OnSelected += SelectEmployee1OnSelected;
            Load += (OrderReclaimDetail_Load);
        }

        void OrderReclaimDetail_Load(object sender, EventArgs e)
        {
            imageHandleType.Properties.Items.Add(new ImageComboBoxItem("重新回到库存", ReclaimProductHandleType.BackStore));
            imageHandleType.Properties.Items.Add(new ImageComboBoxItem("报废", ReclaimProductHandleType.Delete));
            imageHandleType.SelectedIndex = 0;

            if (CurrentOrder == null) return;
            labOrderitem.Text = "退货订单项：" + OrderItemCaption;

            IList<Guid> keys = new List<Guid>();
            foreach (OrderReclaimLog log in CurrentOrder.OrderReclaimList)
                keys.Add(log.PrimaryKey);
            IList<OrderItem> items = new List<OrderItem>();
            foreach (OrderItem item in CurrentOrder.OrderDetail)
            {
                if (keys.Contains(item.PrimaryKey)) continue;
                items.Add(item);
            }
            bindingSource1.DataSource = items;
        }

        public Order CurrentOrder;
        public string OrderItemCaption = "";

        protected override BusinessObject InitBusinessObject(Guid currObjKey)
        {
            return Operation.FinancialSystem.GetExpenditureDetail(currObjKey);
        }

        protected override BusinessObject CreateNewBusinessObject()
        {
            return new OrderReclaimLog();
        }

        protected override string GetTitle()
        {
            return "订单退货";
        }

        protected override void DataBinding()
        {
            BindingControl(popEditHandler, "EditValue", "Handler");
            BindingControl(txtReclaimCount, "EditValue", "ReclaimCount");
            BindingControl(txtGivebackMoney, "EditValue", "GivebackMoney");
            BindingControl(txtMemo, "EditValue", "Memo");
            BindingControl(dateLogDate, "EditValue", "LogDate");
            BindingControl(imageHandleType, "EditValue", "HandleType");
        }

        protected override bool IsVail()
        {
            if (popEditHandler.EditValue == null)
            {
                Functions.ShowMessageBox("请输入经手人");
                return false;
            }
            
            return true;
        }

        protected override DetailFormCurrentStat GetDefaultStat()
        {
            return DetailFormCurrentStat.ViewStat;
        }

        protected override void Save(BusinessObject currentObj)
        {
            if (CurrentOrder == null) return;
            if (!(currentObj is OrderReclaimLog)) return;
            OrderReclaimLog opl = currentObj as OrderReclaimLog;
            if (bindingSource1.Current != null)
                opl.ReclaimItem = bindingSource1.Current as OrderItem;
            else
            {
                Functions.ShowMessageBox("请在列表中选择退货项");
                return;
            }

            switch (CurrentStat)
            {
                case DetailFormCurrentStat.AddStat:
                    //Operation.DistrbutionSystem.CreateOrderReclaimlog(opl);
                    Operation.DistrbutionSystem.OrderReclaim(CurrentOrder.PrimaryKey,new []{opl});
                    break;
                case DetailFormCurrentStat.EditStat:
                    Operation.DistrbutionSystem.UpdateOrderReclaimlog(opl);
                    break;
                default:
                    break;
            }
        }

        void SelectEmployee1OnSelected(object sender, EventArgs e)
        {
            popEditHandler.EditValue = selectEmployee1.GetSelectedEmployee;

            if (popupContainerControl1.OwnerEdit != null)
                popEditHandler.ClosePopup();
        }

        private void popupContainerEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (e.Value == null)
                e.DisplayText = "未指定";
            else
            {
                Employee employee = e.Value as Employee;
                e.DisplayText = employee != null ? employee.EmpName : "未指定";
            }
        }

        private void gridControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

        }

        private void repositoryItemTextEdit1_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            if (e.Value != null)
            {
                CompanyProductView view = e.Value as CompanyProductView;
                e.DisplayText = view != null ? view.Store.Products.Caption : "";
            }
            else e.DisplayText = "";
        }

        private void bindingSource1_PositionChanged(object sender, EventArgs e)
        {
            if (bindingSource1.Current != null)
            {
                labOrderitem.Text = "退货订单项：" + ((OrderItem)bindingSource1.Current).BookProduct.Store.Products.Caption;
            }
        }
    }
}